package system

import (
	"cubeAdmin/common/jwtauth"
	"cubeAdmin/common/utils/gconv"
	"cubeAdmin/controllers"
	"cubeAdmin/dto"
	"cubeAdmin/models"
	"encoding/json"
	"github.com/beego/beego/v2/core/logs"
	"github.com/beego/beego/v2/core/validation"
	"log"
	"strconv"
)

// RoleController 角色 API
type RoleController struct {
	controllers.BaseController
}

func (c *RoleController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
}

// Index 获取菜单列表视图
// @router /index [get]
func (c *RoleController) Index() {
	c.Data["pageTitle"] = "角色列表"
	user, _ := jwtauth.GetAdminDetailUser(c.Ctx.Input)
	c.Data["user"] = user
	c.TplName = "system/role/main.tpl"
}

// Add 权限添加页面
// @router /add [get]
func (c *RoleController) Add() {
	//list := models.GetRoleList()
	//jobList := models.GetAllJobList()
	//c.Data["RolesList"] = list
	//c.Data["JobList"] = jobList
	c.Data["title"] = "新增权限"
	c.TplName = "system/role/add.tpl"
}

// GetOne @Title 获取单个角色
// @Description 获取单个角色
// @Param    id        path     int    true        "角色ID"
// @Success 200 {object} system.Role
// @router /:id [get]
func (c *RoleController) GetOne() {
	id := c.Ctx.Input.Param(":id")
	id64, _ := strconv.ParseInt(id, 10, 64)
	role := models.GetOneRole(id64)
	c.Ok(role)
}

// GetAll @Title 角色列表
// @Description 角色列表
// @Success 200 {object} controllers.Result
// @router / [get]
func (c *RoleController) GetAll() {
	total, list := models.GetAllRole(c.GetParams())
	c.PageTable(gconv.Int64(total), list)
}

// GetRoleMenu 获取角色权限
// @router /roleMenus [get]
func (c *RoleController) GetRoleMenu() {
	var sys models.SysRoleMenu
	roleId, _ := c.GetInt("roleId")
	list := models.SelectMenuList("")
	menuIds, err := sys.GetRoleMenuId(roleId)
	if err != nil {
		log.Print(err)
	}
	for k := range list {
		for k1 := range menuIds {
			if list[k].Id == menuIds[k1] {
				list[k].CheckArr = "1"
			}
		}
	}
	c.DataTree(list)
}

// Post @Title 角色添加
// @Description 角色添加
// @Success 200 {object} controllers.Result
// @router / [post]
func (c *RoleController) Post() {
	var model models.SysRole
	valid := validation.Validation{}
	json.Unmarshal(c.Ctx.Input.RequestBody, &model)
	b, _ := valid.Valid(&model)
	if !b {
		for _, err := range valid.Errors {
			c.Fail(err.Message, 5001)
		}
	}
	_, e := models.AddRole(&model)
	if e != nil {
		c.Fail(e.Error(), 5002)
	}
	c.Ok("操作成功")
}

// Put 跳转到用户编辑
// @router / [put]
func (c *RoleController) Put() {
	var model models.SysRole
	valid := validation.Validation{}
	json.Unmarshal(c.Ctx.Input.RequestBody, &model)
	b, _ := valid.Valid(&model)
	if !b {
		for _, err := range valid.Errors {
			c.Fail(err.Message, 5003)
		}
	}
	e := models.UpdateByRole(&model)
	if e != nil {
		c.Fail(e.Error(), 5004)
	}
	c.Ok("操作成功")
}

// Edit 跳转到用户编辑
// @router /edit [get]
func (c *RoleController) Edit() {
	uid, _ := c.GetInt64("roleId")
	role := models.GetOneRole(uid)
	c.Data["title"] = "角色修改"
	c.Data["role"] = role
	c.TplName = "system/role/edit.tpl"
}

// MenuIndex 跳转到用户编辑
// @router /menuIndex [get]
func (c *RoleController) MenuIndex() {
	uid, _ := c.GetInt64("roleId")
	role := models.GetOneRole(uid)
	c.Data["title"] = "角色授权"
	c.Data["role"] = role
	c.TplName = "system/role/menu.tpl"
}

// Delete @Title 角色删除
// @Description 角色删除
// @Success 200 {object} controllers.Result
// @router / [delete]
func (c *RoleController) Delete() {
	var ids []int64
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &ids)
	if err != nil {
		c.Fail("数据转换异常", 5005)
	}
	logs.Info(ids)
	e := models.DelByRole(ids)
	if e != nil {
		c.Fail(e.Error(), 5005)
	}
	c.Ok("操作成功")
}

// Menu @Title 角色菜单更新
// @Description 角色菜单更新
// @Success 200 {object} controllers.Result
// @router /menu [put]
func (c *RoleController) Menu() {
	var model dto.RoleMenu
	valid := validation.Validation{}
	body := c.Ctx.Input.RequestBody
	if err := json.Unmarshal(body, &model); err != nil {
		logs.Error("数据反序列化失败:", err)
	}
	logs.Info("=======menu======")
	logs.Info(model)
	b, _ := valid.Valid(&model)
	if !b {
		for _, err := range valid.Errors {
			c.Fail(err.Message, 5006)
		}
	}
	models.BatchRoleMenuAdd(model)
	c.Ok("操作成功")
}
